__author__ = 'zoulida'


import pandas as pd
import talib #pip install TA_Lib-0.4.17-cp36-cp36m-win_amd64.whl   https://www.lfd.uci.edu/~gohlke/pythonlibs/#ta-lib
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import mpl_finance as mpf
fig = plt.figure(figsize=(12, 8))
plt.rcParams['font.sans-serif'] = ['SimHei']
ax = fig.add_subplot(111)
df = pd.read_excel("歌尔股份k.xlsx")
df['date'] = pd.to_datetime(df['date'])
df['date'] = df['date'].apply(lambda x: x.strftime('%Y-%m-%d'))
df['star'] = talib.CDLDOJISTAR(df['open'].values, df['high'].values, df['low'].values, df['close'].values)

pattern = df[(df['star'] == 100) | (df['star'] == -100)]

mpf.candlestick2_ochl(ax, df["open"], df["close"], df["high"], df["low"], width=0.6, colorup='r',
                      colordown='green',
                      alpha=1.0)
for key, val in df.items():
    for index, today in pattern.iterrows():
        x_posit = df.index.get_loc(index)
        ax.annotate("{}\n{}".format("十字星", today["date"]), xy=(x_posit, today["high"]),
                    xytext=(0, pattern["close"].mean()), xycoords="data",
                    fontsize=18, textcoords="offset points", arrowprops=dict(arrowstyle="simple", color="r"))


ax.xaxis.set_major_locator(ticker.MaxNLocator(20))

def format_date(x, pos=None):
    if x < 0 or x > len(df['date']) - 1:
        return ''
    return df['date'][int(x)]


ax.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
plt.show()
